#include "service_registry.h"

#include <mutex>

const sptr<ServiceRegistry> &ServiceRegistry::GetInstance()
{
    if (instance == nullptr) {
        static std::mutex mutex;
        std::lock_guard<std::mutex> lock(mutex);
        if (instance == nullptr) {
            instance = new ServiceRegistry();
        }
    }
    return instance;
}

void ServiceRegistry::AddService(const sptr<IService> &service)
{
    services[service->GetID()] = service;
}

void ServiceRegistry::RemoveService(const sptr<IService> &service)
{
    services.erase(service->GetID());
}

std::string ServiceRegistry::DispatchMessage(int id, const std::string &message)
{
    return services[id]->Process(message);
}
